(ns db.routingtest
  (:use clojure.test
	clj.jtacck.db.routing))

(deftest make-directions-api-url-test
  (are [params url]
       (= (apply make-directions-api-url params) url)
       ["98105" "Seattle, WA" ["New York, NY" "Los Angeles, CA" "Houston, TX"]]
       "https://maps.googleapis.com/maps/api/directions/json?origin=98105&destination=Seattle%2C+WA&waypoints=New+York%2C+NY%7CLos+Angeles%2C+CA%7CHouston%2C+TX&sensor=false&"
       ["ORIGIN" "DESTINATION" ["WAY 1" "WAY 2"]]
       "https://maps.googleapis.com/maps/api/directions/json?origin=ORIGIN&destination=DESTINATION&waypoints=WAY+1%7CWAY+2&sensor=false&"
       ["98195" "98105" []]
       "https://maps.googleapis.com/maps/api/directions/json?origin=98195&destination=98105&waypoints=&sensor=false&"
       ["98195" "98105" [] :avoid "tolls" :mode "walking"]
       "https://maps.googleapis.com/maps/api/directions/json?origin=98195&destination=98105&waypoints=&sensor=false&avoid=tolls&mode=walking&"))

(deftest get-routing-test
  (let [route1 (get-routing "98195" "98105" [] :mode "walking")]
    (is (= (:status route1) "OK"))
    (is (= (count (:routes route1)) 1))
    (is (= (count (get-in route1 [:routes 0 :legs])) 1))
    (is (= (get-in route1 [:routes 0 :legs 0 :start_address])
	   "Seattle, WA 98195, USA"))
    (is (= (get-in route1 [:routes 0 :legs 0 :end_address])
	   "Seattle, WA 98105, USA"))
    (is (not (empty? (for [leg (get-in route1 [:routes 0 :legs])
			   step (:steps leg)]
		       (is (= (:travel_mode step) "WALKING"))))))))

(deftest get-total-duration-test
  (let [route1 (get-routing "98195" "98105" [] :mode "walking")
	duration1 (first (get-total-duration route1))
	route2 (get-routing "98105" "98208" [] :mode "driving")
	duration2 (first (get-total-duration route2))]
    (is (and (> duration1 2100) (< duration1 3000)))
    (is (and (> duration2 1000) (< duration2 3000)))))

(comment ; TODO Testing DB
  (deftest get-courier-destination-test
    (is (not "not implemented yet")))
  
  (deftest get-courier-etas-test
    (is (not "not implemented yet")))
  
  (deftest get-specimen-etas-test
    (is (not "not implemented yet"))))